package com.acedemo.game2048.app;

import com.acedemo.game2048.LogUtil;
import com.acedemo.game2048.MyApplication;
import com.acedemo.game2048.ResourceTable;
import ohos.app.Context;
import ohos.media.audio.AudioManager;
import ohos.media.audio.SoundPlayer;

public class SoundManager {
    public static SoundManager instance;
    private static final String AUDIO_PATH = "resources/rawfile/game_2048_volume.mp3";
    private Context mContext;
    private SoundPlayer player;
    private int soundId;

    public static SoundManager newInstance(Context context) {
        if (instance == null) {
            instance = new SoundManager(context);
        }
        return instance;
    }

    private SoundManager(Context context) {
        super();
        this.mContext = context;
        initSoundPool(context);
    }

    private void initSoundPool(Context context) {
        player = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
        LogUtil.loge("=== initSoundPool 1");
        soundId = player.createSound(context, ResourceTable.Media_game_2048_volume);
    }

    public void playSound() {
        LogUtil.loge("=== playSound 1: " + soundId);
        SoundPlayer.SoundPlayerParameters parameters = new SoundPlayer.SoundPlayerParameters();
        SoundPlayer.AudioVolumes audioVolumes = new SoundPlayer.AudioVolumes();
        audioVolumes.setFrontLeftVolume(1.0f);
        audioVolumes.setFrontRightVolume(1.0f);
        parameters.setVolumes(audioVolumes);
        parameters.setLoop(0);
        parameters.setPriority(1);
        parameters.setSpeed(1f);
        player.play(soundId, parameters);
        LogUtil.loge("=== playSound 2: " + soundId);
    }

    public void release() {
        LogUtil.loge("=== release sound");
        if (player != null) {
            player.deleteSound(soundId);
        }
    }


}
